1include <../std.scad>
  2include <../strings.scad>
  3
  4
  5module test_upcase() {
  6    assert(upcase("") == "");
  7    assert(upcase("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  8    assert(upcase("abcdefghijklmnopqrstuvwxyz") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  9    assert(upcase("1234567890!@#$%^&*()") == "1234567890!@#$%^&*()");
 10    assert(upcase("_+-=[]\\{}|;':\",./<>?`~") == "_+-=[]\\{}|;':\",./<>?`~");
 11}
 12test_upcase();
 13
 14
 15module test_downcase() {
 16    assert(downcase("") == "");
 17    assert(downcase("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == "abcdefghijklmnopqrstuvwxyz");
 18    assert(downcase("abcdefghijklmnopqrstuvwxyz") == "abcdefghijklmnopqrstuvwxyz");
 19    assert(downcase("1234567890!@#$%^&*()") == "1234567890!@#$%^&*()");
 20    assert(downcase("_+-=[]\\{}|;':\",./<>?`~") == "_+-=[]\\{}|;':\",./<>?`~");
 21}
 22test_downcase();
 23
 24module test_substr_match(){
 25     assert(substr_match("abcde",2,"cd")); 
 26     assert(!substr_match("abcde",2,"cx"));
 27     assert(!substr_match("abcde",2,"cdef"));
 28     assert(!substr_match("abcde",-2,"cd"));
 29     assert(!substr_match("abcde",19,"cd"));
 30     assert(substr_match("abc",1,""));
 31     assert(!substr_match("",0,"a"));
 32     assert(substr_match("",0,""));
 33}
 34
 35
 36module test_starts_with() {
 37    assert(!starts_with("", "abc"));
 38    assert(!starts_with("", "123"));
 39    assert(!starts_with("defabc", "abc"));
 40    assert(!starts_with("123def", "def"));
 41    assert(!starts_with("def123def", "123"));
 42    assert(starts_with("abcdef", "abc"));
 43    assert(starts_with("abcabc", "abc"));
 44    assert(starts_with("123def", "123"));
 45}
 46test_starts_with();
 47
 48
 49module test_ends_with() {
 50    assert(!ends_with("", "abc"));
 51    assert(!ends_with("", "123"));
 52    assert(!ends_with("abcdef", "abc"));
 53    assert(ends_with("defabc", "abc"));
 54    assert(ends_with("abcabc", "abc"));
 55    assert(ends_with("def123", "123"));
 56}
 57test_ends_with();
 58
 59
 60module test_format_int() {
 61    assert(format_int(0,6) == "000000");
 62    assert(format_int(3,6) == "000003");
 63    assert(format_int(98765,6) == "098765");
 64    assert(format_int(-3,6) == "-000003");
 65    assert(format_int(-98765,6) == "-098765");
 66}
 67test_format_int();
 68
 69
 70module test_format_fixed() {
 71    assert(format_fixed(-PI*100,8) == "-314.15926536");
 72    assert(format_fixed(-PI,8) == "-3.14159265");
 73    assert(format_fixed(-3,8) == "-3.00000000");
 74    assert(format_fixed(3,8) == "3.00000000");
 75    assert(format_fixed(PI*100,8) == "314.15926536");
 76    assert(format_fixed(PI,8) == "3.14159265");
 77    assert(format_fixed(0,8) == "0.00000000");
 78    assert(format_fixed(-PI*100,3) == "-314.159");
 79    assert(format_fixed(-PI,3) == "-3.142");
 80    assert(format_fixed(-3,3) == "-3.000");
 81    assert(format_fixed(3,3) == "3.000");
 82    assert(format_fixed(PI*100,3) == "314.159");
 83    assert(format_fixed(PI,3) == "3.142");
 84}
 85test_format_fixed();
 86
 87
 88module test_format_float() {
 89    assert(format_float(-PI*100,8) == "-314.15927");
 90    assert(format_float(-PI,8) == "-3.1415927");
 91    assert(format_float(-3,8) == "-3");
 92    assert(format_float(3,8) == "3");
 93    assert(format_float(PI*100,8) == "314.15927");
 94    assert(format_float(PI,8) == "3.1415927");
 95    assert(format_float(0,8) == "0");
 96    assert(format_float(-PI*100,3) == "-314");
 97    assert(format_float(-PI,3) == "-3.14");
 98    assert(format_float(-3,3) == "-3");
 99    assert(format_float(3,3) == "3");
100    assert(format_float(PI*100,3) == "314");
101    assert(format_float(PI,3) == "3.14");
102}
103test_format_float();
104
105
106module test_is_digit() {
107    for (i=[32:126]) {
108        if (i>=ord("0") && i <=ord("9")) {
109            assert(is_digit(chr(i)));
110        } else {
111            assert(!is_digit(chr(i)));
112        }
113    }
114    assert(!is_digit("475B3"));
115    assert(is_digit("478"));
116}
117test_is_digit();
118
119
120module test_is_hexdigit() {
121    for (i=[32:126]) {
122        if (
123            (i>=ord("0") && i <=ord("9")) ||
124            (i>=ord("A") && i <=ord("F")) ||
125            (i>=ord("a") && i <=ord("f"))
126        ) {
127            assert(is_hexdigit(chr(i)));
128        } else {
129            assert(!is_hexdigit(chr(i)));
130        }
131    }
132}
133test_is_hexdigit();
134
135
136module test_is_letter() {
137    for (i=[32:126]) {
138        if (
139            (i>=ord("A") && i <=ord("Z")) ||
140            (i>=ord("a") && i <=ord("z"))
141        ) {
142            assert(is_letter(chr(i)));
143        } else {
144            assert(!is_letter(chr(i)));
145        }
146    }
147}
148test_is_letter();
149
150
151module test_is_lower() {
152    for (i=[32:126]) {
153        if (
154            (i>=ord("a") && i <=ord("z"))
155        ) {
156            assert(is_lower(chr(i)));
157        } else {
158            assert(!is_lower(chr(i)));
159        }
160    }
161    assert(is_lower("abcdefghijklmnopqrstuvwxyz"));
162    assert(!is_lower("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
163    assert(!is_lower("abcdefghijKlmnopqrstuvwxyz"));
164    assert(!is_lower("abcdefghijklmnopqrstuvwxyZ"));
165}
166test_is_lower();
167
168
169module test_is_upper() {
170    for (i=[32:126]) {
171        if (
172            (i>=ord("A") && i <=ord("Z"))
173        ) {
174            assert(is_upper(chr(i)));
175        } else {
176            assert(!is_upper(chr(i)));
177        }
178    }
179    assert(is_upper("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
180    assert(!is_upper("abcdefghijklmnopqrstuvwxyz"));
181    assert(!is_upper("ABCDEFGHIJkLMNOPQRSTUVWXYZ"));
182    assert(!is_upper("ABCDEFGHIJKLMNOPQRSTUVWXYz"));
183}
184test_is_upper();
185
186
187module test_parse_float() {
188    assert(parse_float("3.1416") == 3.1416);
189    assert(parse_float("-3.1416") == -3.1416);
190    assert(parse_float("3.000") == 3.0);
191    assert(parse_float("-3.000") == -3.0);
192    assert(parse_float("3") == 3.0);
193    assert(parse_float("0") == 0.0);
194}
195test_parse_float();
196
197
198module test_parse_frac() {
199    assert(parse_frac("") == 0);
200    assert(parse_frac("1/2") == 1/2);
201    assert(parse_frac("+1/2") == 1/2);
202    assert(parse_frac("-1/2") == -1/2);
203    assert(parse_frac("7/8") == 7/8);
204    assert(parse_frac("+7/8") == 7/8);
205    assert(parse_frac("-7/8") == -7/8);
206    assert(parse_frac("1 1/2") == 1 + 1/2);
207    assert(parse_frac("+1 1/2") == 1 + 1/2);
208    assert(parse_frac("-1 1/2") == -(1 + 1/2));
209    assert(parse_frac("768 3/4") == 768 + 3/4);
210    assert(parse_frac("+768 3/4") == 768 + 3/4);
211    assert(parse_frac("-768 3/4") == -(768 + 3/4));
212    assert(parse_frac("19") == 19);
213    assert(parse_frac("+19") == 19);
214    assert(parse_frac("-19") == -19);
215    assert(parse_frac("3/0") == INF);
216    assert(parse_frac("-3/0") == -INF);
217    assert(is_nan(parse_frac("0/0")));
218    assert(is_nan(parse_frac("-77/9", improper=false)));
219    assert(is_nan(parse_frac("-2 12/4",improper=false))); 
220    assert(is_nan(parse_frac("-2 12/4",signed=false)));   
221    assert(is_nan(parse_frac("-2 12/4",mixed=false)));    
222    assert(is_nan(parse_frac("2 1/4",mixed=false)));
223    assert(is_nan(parse_frac("2", mixed=false)));
224}
225test_parse_frac();
226
227
228module test_parse_num() {
229    assert(parse_num("") == 0);
230    assert(parse_num("1/2") == 1/2);
231    assert(parse_num("+1/2") == 1/2);
232    assert(parse_num("-1/2") == -1/2);
233    assert(parse_num("7/8") == 7/8);
234    assert(parse_num("+7/8") == 7/8);
235    assert(parse_num("-7/8") == -7/8);
236    assert(parse_num("1 1/2") == 1 + 1/2);
237    assert(parse_num("+1 1/2") == 1 + 1/2);
238    assert(parse_num("-1 1/2") == -(1 + 1/2));
239    assert(parse_num("768 3/4") == 768 + 3/4);
240    assert(parse_num("+768 3/4") == 768 + 3/4);
241    assert(parse_num("-768 3/4") == -(768 + 3/4));
242    assert(parse_num("19") == 19);
243    assert(parse_num("+19") == 19);
244    assert(parse_num("-19") == -19);
245    assert(parse_num("3/0") == INF);
246    assert(parse_num("-3/0") == -INF);
247    assert(parse_num("3.14159") == 3.14159);
248    assert(parse_num("-3.14159") == -3.14159);
249    assert(is_nan(parse_num("0/0")));
250}
251test_parse_num();
252
253
254module test_parse_int() {
255    assert(parse_int("0") == 0);
256    assert(parse_int("3") == 3);
257    assert(parse_int("7655") == 7655);
258    assert(parse_int("+3") == 3);
259    assert(parse_int("+7655") == 7655);
260    assert(parse_int("-3") == -3);
261    assert(parse_int("-7655") == -7655);
262    assert(parse_int("ffff",16) == 65535);
263}
264test_parse_int();
265
266
267module test_str_join() {
268    assert(str_join(["abc", "D", "ef", "ghi"]) == "abcDefghi");
269    assert(str_join(["abc", "D", "ef", "ghi"], "--") == "abc--D--ef--ghi");
270}
271test_str_join();
272
273
274module test_str_split() {
275    assert(str_split("abc-def+ghi-jkl", "-") == ["abc","def+ghi","jkl"]);
276    assert(str_split("abc-def+ghi-jkl", "-+") == ["abc","def","ghi","jkl"]);
277    assert(str_split("abc--def-ghi", "-", true) == ["abc","","def","ghi"]);
278    assert(str_split("abc--def-ghi", "-", false) == ["abc","def","ghi"]);
279    assert(str_split("abc-+def-ghi", "-+", true) == ["abc","","def","ghi"]);
280    assert(str_split("abc-+def-ghi", "-+", false) == ["abc","def","ghi"]);
281}
282test_str_split();
283
284
285module test_str_strip() {
286    assert(str_strip("abcdef", " ") == "abcdef");
287    assert(str_strip(" abcdef", " ") == "abcdef");
288    assert(str_strip("  abcdef", " ") == "abcdef");
289    assert(str_strip("abcdef ", " ") == "abcdef");
290    assert(str_strip("abcdef  ", " ") == "abcdef");
291    assert(str_strip(" abcdef  ", " ") == "abcdef");
292    assert(str_strip("  abcdef  ", " ") == "abcdef");
293    assert(str_strip("abcdef", " ",start=true) == "abcdef");
294    assert(str_strip(" abcdef", " ",start=true) == "abcdef");
295    assert(str_strip("  abcdef", " ",start=true) == "abcdef");
296    assert(str_strip("abcdef ", " ",start=true) == "abcdef ");
297    assert(str_strip("abcdef  ", " ",start=true) == "abcdef  ");
298    assert(str_strip(" abcdef  ", " ",start=true) == "abcdef  ");
299    assert(str_strip("  abcdef  ", " ",start=true) == "abcdef  ");
300    assert(str_strip("abcdef", " ",end=true) == "abcdef");
301    assert(str_strip(" abcdef", " ",end=true) == " abcdef");
302    assert(str_strip("  abcdef", " ",end=true) == "  abcdef");
303    assert(str_strip("abcdef ", " ",end=true) == "abcdef");
304    assert(str_strip("abcdef  ", " ",end=true) == "abcdef");
305    assert(str_strip(" abcdef  ", " ",end=true) == " abcdef");
306    assert(str_strip("  abcdef  ", " ",end=true) == "  abcdef");
307    assert(str_strip("123abc321","12") == "3abc3");
308    assert(str_strip("123abc321","12",start=true,end=true) == "3abc3");
309    assert(str_strip("123abc321","12",start=true,end=false) == "3abc321");    
310    assert(str_strip("123abc321","12",start=false,end=false) == "123abc321");    
311    assert(str_strip("123abc321","12",start=false,end=true) == "123abc3");    
312    assert(str_strip("123abc321","12",start=false) == "123abc3");    
313    assert(str_strip("123abc321","12",start=true) == "3abc321");    
314    assert(str_strip("123abc321","12",end=false) == "3abc321");
315    assert(str_strip("123abc321","12",end=true) == "123abc3");
316    assert(str_strip("abcde","abcde")=="");
317    assert(str_strip("","abc")=="");
318}
319test_str_strip();
320
321
322module test_substr() {
323    assert(substr("abcdefg",3,3) == "def");
324    assert(substr("abcdefg",2) == "cdefg");
325    assert(substr("abcdefg",len=3) == "abc");
326    assert(substr("abcdefg",[2,4]) == "cde");
327    assert(substr("abcdefg",len=-2) == "");
328}
329test_substr();
330
331
332module test_suffix() {
333    assert(suffix("abcdefghi",0) == "");
334    assert(suffix("abcdefghi",1) == "i");
335    assert(suffix("abcdefghi",2) == "hi");
336    assert(suffix("abcdefghi",3) == "ghi");
337    assert(suffix("abcdefghi",6) == "defghi");
338    assert(suffix("abc",4) == "abc");
339}
340test_suffix();
341
342
343module test_str_find() {
344    assert(str_find("abc123def123abc","123") == 3);
345    assert(str_find("abc123def123abc","b") == 1);
346    assert(str_find("abc123def123abc","1234") == undef);
347    assert(str_find("abc","") == 0);
348    assert(str_find("abc123def123", "123", start=4) == 9);
349    assert(str_find("abc123def123abc","123",last=true) == 9);
350    assert(str_find("abc123def123abc","b",last=true) == 13);
351    assert(str_find("abc123def123abc","1234",last=true) == undef);
352    assert(str_find("abc","",last=true) == 3);
353    assert(str_find("abc123def123", "123", start=8, last=true) == 3);
354    assert(str_find("abc123def123abc","123",all=true) == [3,9]);
355    assert(str_find("abc123def123abc","b",all=true) == [1,13]);
356    assert(str_find("abc123def123abc","1234",all=true) == []);
357    assert(str_find("abc","",all=true) == [0,1,2]);
358}
359test_str_find();
360
361
362module test_format() {
363    assert(format("The value of {} is {:.14f}.", ["pi", PI]) == "The value of pi is 3.14159265358979.");
364    assert(format("The value {1:f} is known as {0}.", ["pi", PI]) == "The value 3.141593 is known as pi.");
365    assert(format("We use a very small value {1:.6g} as {0}.", ["EPSILON", EPSILON]) == "We use a very small value 1e-9 as EPSILON.");
366    assert(format("{:-5s}{:i}{:b}", ["foo", 12e3, 5]) == "foo  12000true");
367    assert(format("{:-10s}{:.3f}", ["plecostamus",27.43982]) == "plecostamus27.440");
368    assert(format("{:-10.9s}{:.3f}", ["plecostamus",27.43982]) == "plecostam 27.440");
369}
370test_format();
371
372
373/*
374module test_echofmt() {
375}
376test_echofmt();
377*/
378
379
380module test_str_pad() {
381   assert_equal(str_pad("abc",5,"x"), "abcxx");
382   assert_equal(str_pad("abc",5), "abc  ");
383   assert_equal(str_pad("abc",5,"x",left=true), "xxabc");
384   assert_equal(str_pad("", 5, "x"), "xxxxx");
385   assert_equal(str_pad("", 5, "x", left=true), "xxxxx");
386}   
387test_str_pad();
388
389module test_str_replace_char() {
390   assert_equal(str_replace_char("abcabc", "b", "xyz"), "axyzcaxyzc");
391   assert_equal(str_replace_char("abcabc", "b", ""), "acac");
392   assert_equal(str_replace_char("", "b", "xyz"), "");
393   assert_equal(str_replace_char("acdacd", "b", "xyz"), "acdacd");   
394}
395test_str_replace_char();
396
397
398
399// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap